/*
**********************************************************************************
    OpenSURF642 : An TI DM642 implementation of OpenSURF.
    Copyright (C) 2011  Hai Yu

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
	This implementation is based on The original OpenSURF library created
	by Christopher Evans.The original work can be found at
		http://www.chrisevansdev.com/computer-vision-opensurf.html
	
    contact: yuhaihai12345@gmail.com	
**********************************************************************************
*/

#include "OpenSurf642util.h"
#include <assert.h>

IntegralImage * CreateIntegralImage(Uimage8  * src)
{	
	int i,j;

	IntegralImage * des = (IntegralImage *)malloc(sizeof(IntegralImage));
	IntegralDataType temp_val = 0;	
	Uint8 * img;
	IntegralDataType * integral; 

	Uint16 lines = src->lines;
	Uint16 pixels = src->pixels_per_line;

	des->data = (IntegralDataType *)malloc((lines+1) * (pixels+2) * sizeof(IntegralDataType));
	des->lines = src->lines;
	des->pixels_per_line = src->pixels_per_line;
	img = src->data;
	integral = des->data;

	assert(src->lines == des->lines);
	assert(src->pixels_per_line == des->pixels_per_line);
	//First line -- all zero
	for(j = 0; j < pixels + 2; j++)
	{
		integral[j] = 0;
	}
	//Second line
	integral[pixels + 2] = 0;
	integral[pixels + 3] = 0;
	temp_val = 0;
	for(j = 0; j < pixels; j++)
	{
		temp_val = temp_val + img[j];
		integral[j+4+pixels] = temp_val;
	}
	//Rest lines
	for(i = 1;i < lines; i++)
	{
		temp_val = 0;
		integral[(i+1)*(pixels+2)]=0 ;
		integral[(i+1)*(pixels+2)+1]=0 ;
		for(j = 0; j < pixels; j ++)
		{
			temp_val = temp_val	+ img[i*pixels + j];
			integral[(i+1)*(pixels+2) + j + 2] = temp_val + integral[(i)*(pixels+2) + j+2];
		}
	
	}
	return des;

}

